package com.aiobc.file.controller;

import com.aiobc.file.dto.BaseResponse;
import com.aiobc.file.dto.FileUploadResp;
import com.aiobc.file.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Slf4j
@RestController
@Api("文件上传")
@RequestMapping("/file")
public class FileController {

	@Autowired
	private FileService fileService;

	/**
	 * 上传单个文件<br/>
	 */
	@ApiOperation(value = "单个文件上传", httpMethod = "POST")
	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	public BaseResponse<FileUploadResp> uploadSingle(
			@ApiParam(value = "file", required = true) @RequestParam("file") MultipartFile file) {
		return BaseResponse.createSuccess(fileService.uploadSingle(file));

	}

	/**
	 * 上传文件<br/>
	 */
	@ApiOperation(value = "批量文件上传", httpMethod = "POST")
	@RequestMapping(value = "/uploadBatch", method = RequestMethod.POST)
	public BaseResponse<List<FileUploadResp>> uploadBatch(
			@ApiParam(value = "files", required = true) @RequestParam("files") MultipartFile[] files) {
		return BaseResponse.createSuccess(fileService.uploadBatch(files));
	}

	@GetMapping("/download")
	public void downLoad(HttpServletResponse response, @RequestParam("fileName") String fileName) {
		fileService.downLoad(response, fileName);
	}

}
